home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / rss / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-08  |  3.6 KB  |  132 lines

  1. /*                              G E T O P T . C
  2.  
  3.     % 1 name
  4. \functoc {getopt}
  5.     % 2 declaration
  6. {int \fname (\params\ )}
  7.     % 3 arguments
  8. {
  9.     {int *}{p_argc}{pointer to argument counter}
  10.     {char **}{argv}{pointer to argument strings}
  11. }
  12.     % 4 return value
  13. {The character value of the next option or -1 if no more options were found}
  14.     % 5 functions used
  15. {}
  16.     % 6 see also
  17. {getoptval, setoptchars, getoptindex}
  18.     % 7 source file
  19. {getopt.c}
  20.     % 8 description
  21. {The function scans the strings in the \Var{argv} array for the next
  22. occurrence of an option character, and returns the character(s) immediately
  23. following it. If no (more) option characters were found -1 is returned. The
  24. value 0 is returned if the separator character is not followed by another
  25. character as in, e.g.,
  26. \begin{center}
  27.     {\tt program -}
  28. \end{center}
  29. The function will reduce the value of \Var{*p_argc}, and will remove the option
  30. from the \Var{argv} array.
  31.  
  32. However, if multiple characters follow the option character (e.g, -more), then
  33. the individual characters are returned as successive option characters (in the
  34. example: {\tt m, o, r, e}, respectively).
  35.  
  36. The switchcharacters may be defined by function \Function{setoptchar}, and
  37. contains initially the characters {\tt `-'} and {\tt `/'}.
  38. }
  39.     % 9 example
  40. {}
  41. \footnotesize
  42. \begin{verbatim}
  43.  
  44. #include <icce.h>
  45.  
  46. void main(int argc, char **argv)
  47. {
  48.     int
  49.         c;
  50.  
  51.     while ((c = getopt(&argc, argv)) != -1)
  52.         process_option(c);
  53.  
  54.     if (argc == 1)
  55.         usage(argv);
  56.  
  57.     rest_of_program();
  58. }
  59.  
  60. \end{verbatim}
  61. \normalsize
  62. */
  63. #ifndef MSDOS
  64.  
  65. #include <stdio.h>
  66. #include <string.h>
  67. #include "icrss.h"
  68.  
  69. char
  70.     *near icce_beyond_optchar,              /* no character beyond optchar yet */
  71.     *near icce_optchar = "-",
  72.     *near icce_optval;
  73.  
  74. int
  75.     near icce_next_option = 0;
  76.  
  77.  
  78. int ic_getopt(int *p_argc, char **argv)
  79. {
  80.     register int
  81.         argc,
  82.         index;
  83.     char
  84.         **org_argv;
  85.  
  86.     while (1)
  87.     {
  88.         if (icce_beyond_optchar)                 /* option found ? */
  89.         {
  90.             if (*icce_optval)              /* return next option character */
  91.                 return (*icce_optval++);
  92.  
  93.             if (!*icce_beyond_optchar)           /* no immediate option char */
  94.             {
  95.                 icce_beyond_optchar = NULL;      /* option processed */
  96.                 return (0);
  97.             }
  98.             else
  99.                 icce_beyond_optchar = NULL;      /* option processed */
  100.         }
  101.  
  102.         org_argv = argv;                        /* save original address */
  103.         argv += icce_next_option;               /* point to next option string */
  104.         argc = *p_argc;                         /* local argc counter */
  105.  
  106.         for
  107.         (                                       /* walk along the options */
  108.             index = icce_next_option;
  109.                 index < argc;                   /* as long as there are args */
  110.                     index++, argv++             /* do next after code */
  111.         )
  112.         {
  113.             if (strchr(icce_optchar, **argv))  /* option found ? */
  114.             {
  115.                 icce_next_option = index;   /* prepare for next call */
  116.  
  117.                 icce_optval = *argv + 1;    /* optval points to option string */
  118.                 icce_beyond_optchar = icce_optval;
  119.  
  120.                 while (++index <= argc)     /* shift the argv's + extra NULL */
  121.                     org_argv[index - 1] = org_argv[index];
  122.                 (*p_argc)--;                /* reduce external count */
  123.             }
  124.         }
  125.  
  126.         if (!icce_beyond_optchar)                /* no (more) options */
  127.             return (-1);
  128.     }
  129. }
  130.  
  131. #endif                        /* MSDOS */
  132.